/**
 * 生成转格式命令
 */

const path = require("path");
const ffmpegPath = path.join(__dirname , '../', 'ffmpeg', 'ffmpeg.exe');

const SuffixMap = new Map([
    // 同名文件
    ['default', `"${ffmpegPath}" -i "@input" -c copy -map 0 "@output"`],
    // mp4--->
    ['mp4-wmv', `"${ffmpegPath}" -i "@input" -c:v wmv2 -b:v 1024k -c:a wmav2 -b:a 192k "@output"`],
    ['mp4-avi', `"${ffmpegPath}" -i "@input" -q:v 6 "@output"`],
    ['mp4-flv', `"${ffmpegPath}" -i "@input" -c:v libx264 -crf 24 "@output"`],
    ['mp4-ts', `"${ffmpegPath}" -y -i "@input" -vcodec copy -acodec copy -vbsf h264_mp4toannexb "@output"`],
    ['mp4-webm', `"${ffmpegPath}" -i "@input" "@output"`],

    // wmv--->
    ['wmv-mp4', `"${ffmpegPath}" -i "@input" "@output"`],

    // avi--->
    ['avi-mp4', `"${ffmpegPath}" -i "@input" -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 "@output"`],

    // flv--->
    ['flv-mp4', `"${ffmpegPath}" -i "@input" -vcodec copy -acodec copy "@output"`],

    // webm--->
    ['webm-mp4', `"${ffmpegPath}" -i "@input" -vf "scale=trunc(iw/2)*2:trunc(ih/2)" "@output"`],

    // ts--->
    ['ts-mp4', `"${ffmpegPath}" -y -i "@input" -c:v libx264 -c:a copy -bsf:a aac_adtstoasc "@output"`],
]);

function setTranscodingCMD(sourceFile, targetFile) {
    // 判断格式
    let sourceSuffix = sourceFile.split(".").pop();
    let targetSuffix = targetFile.split(".").pop();


    let mapStr = `${sourceSuffix}-${targetSuffix}`;
    let cmdStr = '';

    if (sourceSuffix === targetSuffix) {
        cmdStr = SuffixMap.get('default');
    } else {
        cmdStr = SuffixMap.get(mapStr);
    }

    if (!cmdStr) {
        return {
            result: "转换失败",
            msg: `暂不支持 ${sourceSuffix} 转 ${targetSuffix} 格式`
        }
    }

    cmdStr = cmdStr.replace("@input", sourceFile);
    cmdStr = cmdStr.replace("@output", targetFile);

    return cmdStr;

}

module.exports = setTranscodingCMD;